package com.sou.technologytool.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.benben.commoncore.utils.ActivityManagerUtils;
import com.benben.commoncore.utils.AppManager;
import com.benben.commoncore.utils.ScreenUtils;
import com.benben.commoncore.utils.StatusBarUtils;
import com.benben.commoncore.utils.ToastUtils;
import com.sou.technologytool.R;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity {
    protected Activity mContext;
    public boolean isShow;

    Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawableResource(android.R.color.white);
        if (!isTaskRoot()) {
            final Intent intent = getIntent();
            final String intentAction = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent
                    .ACTION_MAIN)) {
                finish();
                return;
            }
        }
        AppManager.getInstance().addActivity(this);
        this.mContext = this;
        if (isUseEventBus() && !EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        setStatusBar(isShow);
        setContentView(getLayoutId());
        unbinder = ButterKnife.bind(this, this);
        initData();
    }

    protected void clearFocus() {
        View focusView = getWindow().getDecorView().getRootView().findFocus();
        if (focusView != null && focusView instanceof EditText) {
            ScreenUtils.closeKeybord((EditText) focusView, mContext);
        }
    }

    public boolean isClickEt(View view, MotionEvent event) {
        if (view != null && (view instanceof EditText)) {
            int[] leftTop = {0, 0};
            view.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + view.getHeight();
            int right = left + view.getWidth();
            if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isClickEt(view, event)) {
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (inputMethodManager != null) {
                    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(event);
        }
        if (getWindow().superDispatchTouchEvent(event)) {
            return true;
        }
        return onTouchEvent(event);
    }


    protected abstract int getLayoutId();

    protected abstract void initData();

    protected void initTitle(String title) {
        TextView tvTitle = findViewById(R.id.center_title);
        tvTitle.setText(title);
        RelativeLayout rlBack = findViewById(R.id.rl_back);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityManagerUtils.remove(this);
                finish();
            }
        });
    }
    public void setTitleColor(int color) {
        TextView tvTitle = findViewById(R.id.center_title);
        tvTitle.setTextColor(color);
    }

    protected void setStatusBar(boolean isShow) {
        if(isShow){
            StatusBarUtils.fullScreen(this,getStatusBarColor());
        }else {
            StatusBarUtils.setStatusBarColor(mContext, getStatusBarColor());
            StatusBarUtils.setAndroidNativeLightStatusBar(mContext, needStatusBarDarkText());
        }
    }

    protected boolean needFitsSystemWindows() {
        return true;
    }

    protected boolean needStatusBarDarkText() {
        return true;
    }

    protected @ColorRes
    int getStatusBarColor() {
        return R.color.white;
    }

    protected @ColorRes
    int getStatusBarRedColor() {
        return R.color.color_FF4359;
    }

    protected @ColorRes
    int getStatusBlackRedColor() {
        return R.color.black;
    }

    public void showProgress() {

    }

    public void closeProgress() {

    }

    public void toast(String strMsg) {
        ToastUtils.show(this, strMsg);
    }

    public void toast(int resid) {
        ToastUtils.show(this, resid);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getInstance().finishActivity(this); //从栈中移除
        overridePendingTransition(0, 0);
        if (isUseEventBus() && EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition(0, 0);
    }
    /**
     * 是否使用 EventBus
     *
     * @return
     */
    protected boolean isUseEventBus() {
        return false;
    }



}
